%% runModelHelper_zf_fits_v2_7
%  Version 2.7
%  Author: Adeyinka Lesi
%  Date: 1/14/21
%  Project: Tumor Growth, Logarithmic Continuum Form
%% Version History
%  1.1: male and female parameters
%  1.2: playing with parameters
%  1.4: comparing multiple parameter sets
%  1.5: comparing varying exp parameters
%  1.6: new data
%  1.7: varying carrying capcity
%  1.8: test parameters
%  1.9: using varying weight thresholds
%  2.0: using runModelFunction_v5_0 - time zero sizes depth > 1
%  2.1: using rev2 data
%  2.6: using newact
%  2.7: using newact
%  2.8: using data segragated by inoc size
%  2.9: sensitivity parameters for mm with inoc size segregation

startt = clock();

conv_int = getLinearConverter(1000);
conv_area = getLinearConverter(100);
conv_vol = getConverter();
select1 = 1:7;
select2 = [1:2 4:8];
select3 = [2:17];
select4 = 2:7;

save_name = 'ct10_lt015_ht04_021720_mm_FisZ_inoc5e6V_0040821_fit1';

% pfems = [
%     3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875,0
%     3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,10.855,0
%     3.3111,0.78887,5.3368,0.00080299,8.8984e-05,8.8984e-08,1,Inf,4.0875,0
%     3.3111,0.78887,5.3368,0.00080299,8.8984e-05,8.8984e-08,1,Inf,10.855,0
%     ];
% pmals = [
%     15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256,0
%     15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,10.363,0
%     15.232,0.67368,14.734,2.7135e-05,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256,0
%     15.232,0.67368,14.734,2.7135e-05,7.8204e-05,7.8204e-08,0.99941,Inf,10.363,0
%     ];
% pfems = [
% 3.3111,0.78887,5.3837,0.00080688,8.8984e-05,8.8984e-08,1,Inf,10.844,0
% 3.3111,0.78887,5.3837,0.00080688,8.8984e-05,8.8984e-08,1,Inf,11.758,0
% 3.3111,0.78887,5.3837,0.00080688,8.8984e-05,8.8984e-08,1,Inf,10.88,0
%     ];
% pmals = [
% 15.232,0.67368,15.118,2.7483e-05,7.8204e-05,7.8204e-08,0.99941,Inf,10.369,0
% 15.232,0.67368,15.118,2.7483e-05,7.8204e-05,7.8204e-08,0.99941,Inf,8.8699,0
% 15.232,0.67368,15.118,2.7483e-05,7.8204e-05,7.8204e-08,0.99941,Inf,10.039,0
%     ];

% pfems = [
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3438e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.0326e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7901,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7784,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.4599e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.2388e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.2395e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,7.8064e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,1.4041e-04,1.4041e-07,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.5840e-05,8.5840e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,0.9973,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,4.1200-4.0401+11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,3.9500-4.0401+11.758,0.0000
%     ];
% pmals = [
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5542e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     4.3640e+00,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6756,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.5349,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.5479e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.3139e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,3.0711e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.5704e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,8.0454e-05,8.0454e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,3.7527e-05,3.7527e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,1.0000,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9478,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,4.4035-4.2539+10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,4.1141-4.2539+10.369,0.0000
%     ];

% pfems = [
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.4599e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.2388e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.2395e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.3368e+00,7.8064e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.4599e+00,7.8064e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     3.3111e+00,0.7889,5.2388e+00,8.2395e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758,0.0000
%     ];
% pmals = [
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.5479e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.3139e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,3.0711e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.5704e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.5479e+01,2.5704e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     1.5232e+01,0.6737,1.3139e+01,3.0711e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369,0.0000
%     ];
% pfems = [
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,4.0875-1,0.0000
%     3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758-1,0.0000
%     ];
% pmals = [
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,4.3256-1,0.0000
%     1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369-1,0.0000
%     ];
pfems = [
    3.3111e+00,0.7889,5.3368e+00,8.0299e-04,8.8984e-05,8.8984e-08,1.0000,  Inf,11.758-1,0.0000
    ];
pmals = [
    1.5232e+01,0.6737,1.4734e+01,2.7135e-05,7.8204e-05,7.8204e-08,0.9994,  Inf,10.369-1,0.0000
    ];

mrs = cell(8,size(pfems,1));
run_list = [5:6];
if(~exist('objs_v1_10','var'))
    objs_v1_10 = zeros(6,size(pfems,1));
else
    if(size(pfems,1)>size(objs_v1_10,2))
        old_objs = objs_v1_10;
        objs_v1_10 = zeros(6,size(pfems,1));
        retain_list = setdiff(1:6,run_list);
        objs_v1_10(retain_list,1:size(old_objs,2)) = old_objs(retain_list,:);
    else
        objs_v1_10(run_list,1:size(pfems,1)) = 0;
    end
end

Nx = 500;
dt = 0.005;

min_surv = 2;
zf2017_inoc = '1e5';
zf2019_inoc = '5e6';
zf2019_inoc2 = '1e6';
% byInocOldAgeVentral_remerged
% byInocOldAgeVentral_remerged
datafile1 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_RAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile2 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile3 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_RAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
datafile4 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
% datafile1 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged_s%i_040521/dist_RAD_zf2017_%s_f_area_vs_count_over_actprim.csv',min_surv,zf2017_inoc);
% datafile2 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged_s%i_040521/dist_NORAD_zf2017_%s_f_area_vs_count_over_actprim.csv',min_surv,zf2017_inoc);
% datafile3 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged_s%i_040521/dist_RAD_zf2017_%s_m_area_vs_count_over_actprim.csv',min_surv,zf2017_inoc);
% datafile4 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged_s%i_040521/dist_NORAD_zf2017_%s_m_area_vs_count_over_actprim.csv',min_surv,zf2017_inoc);
datafile5 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged2_s%i_040521/dist_NORAD_zf2019_%s_f_area_vs_count_over_actprim.csv',min_surv,zf2019_inoc);
datafile6 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged2_s%i_040521/dist_NORAD_zf2019_%s_m_area_vs_count_over_actprim.csv',min_surv,zf2019_inoc);
datafile7 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged2_s%i_040521/dist_NORAD_zf2019_%s_f_area_vs_count_over_actprim.csv',min_surv,zf2019_inoc2);
datafile8 = sprintf('ct10_lt015_ht04_021720_byInocOldAgeVentral_remerged2_s%i_040521/dist_NORAD_zf2019_%s_m_area_vs_count_over_actprim.csv',min_surv,zf2019_inoc2);

stage_sel = 1;

mm_noBack = struct('TIME_ZERO_CALCULATOR_DEPTH',1,'RATE_GENERATOR',@getRates_michaelisMenten_form11_v1_0);
no_backtracking = struct('TIME_ZERO_CALCULATOR_DEPTH',1);
mm_args1 = struct('RATE_GENERATOR',@getRates_michaelisMenten_form11_v1_0);
mm_args2 = struct('RATE_GENERATOR',@getRates_michaelisMenten_form113_v1_0);
mm_args3 = struct('RATE_GENERATOR',@getRates_michaelisMenten_form123_v1_0);
null_args = struct();
args_byRun = {null_args,mm_args3,null_args,mm_args1,mm_args3,mm_args1,mm_args3,mm_args1};
args_byPar = cell(2,size(pfems,1));
for kk = 1:size(args_byPar,2)
    args_byPar{1,kk} = mm_args3;
    args_byPar{2,kk} = mm_args1;
end

for run_sel = run_list
    for i = 1:size(pfems,1)
        switch run_sel
            case 1
                pfem = pfems(i,:);
                params1 = pfem;
                params1([3:4 9:10]) = 0;
                [mrs{run_sel,i},data1,mg_fit1] = runModelFunction_v5_0(datafile1,params1,conv_vol,Nx,dt,select1,args_byPar{1,i},stage_sel);
                mrs{run_sel,i}.data = data1;
                mrs{run_sel,i}.fit = mg_fit1;
                axis([1 5e7 0 max(data1.cdf(end,:))]);
                objs_v1_10(1,i) = timeWeightedObjective_v1_0(mg_fit1,data1.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 2
                pfem = pfems(i,:);
                params2 = pfem;
                [mrs{run_sel,i},data2,mg_fit2] = runModelFunction_v5_0(datafile2,params2,conv_vol,Nx,dt,select2,args_byPar{1,i},3);
                mrs{run_sel,i}.data = data2;
                mrs{run_sel,i}.fit = mg_fit2;
                axis([1 5e7 0 max(data2.cdf(end,:))]);
                objs_v1_10(2,i) = timeWeightedObjective_v1_0(mg_fit2,data2.weights);
            case 3
                pmal = pmals(i,:);
                params3 = pmal;
                params3([3:4 9:10]) = 0;
                [mrs{run_sel,i},data3,mg_fit3] = runModelFunction_v5_0(datafile3,params3,conv_vol,Nx,dt,select1,args_byPar{2,i},stage_sel);
                mrs{run_sel,i}.data = data3;
                mrs{run_sel,i}.fit = mg_fit3;
                axis([1 5e7 0 max(data3.cdf(end,:))]);
                objs_v1_10(3,i) = timeWeightedObjective_v1_0(mg_fit3,data3.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 4
                pmal = pmals(i,:);
                params4 = pmal;
                [mrs{run_sel,i},data4,mg_fit4] = runModelFunction_v5_0(datafile4,params4,conv_vol,Nx,dt,select2,args_byPar{2,i},3);
                mrs{run_sel,i}.data = data4;
                mrs{run_sel,i}.fit = mg_fit4;
                axis([1 5e7 0 max(data4.cdf(end,:))]);
                objs_v1_10(4,i) = timeWeightedObjective_v1_0(mg_fit4,data4.weights);
            case 5
                pfem = pfems(i,:);
                params5 = pfem;
                [mrs{run_sel,i},data5,mg_fit5] = runModelFunction_v5_0(datafile5,params5,conv_vol,Nx,dt,select3,args_byPar{1,i},3);
                mrs{run_sel,i}.data = data5;
                mrs{run_sel,i}.fit = mg_fit5;
                axis([1 5e7 0 max(data5.cdf(end,:))]);
                objs_v1_10(5,i) = timeWeightedObjective_v1_0(mg_fit5,data5.weights);
            case 6
                pmal = pmals(i,:);
                params6 = pmal;
                [mrs{run_sel,i},data6,mg_fit6] = runModelFunction_v5_0(datafile6,params6,conv_vol,Nx,dt,select3,args_byPar{2,i},3);
                mrs{run_sel,i}.data = data6;
                mrs{run_sel,i}.fit = mg_fit6;
                axis([1 5e7 0 max(data6.cdf(end,:))]);
                objs_v1_10(6,i) = timeWeightedObjective_v1_0(mg_fit6,data6.weights);
            case 7
                pfem = pfems(i,:);
                params7 = pfem;
                [mrs{run_sel,i},data7,mg_fit7] = runModelFunction_v5_0(datafile7,params7,conv_vol,Nx,dt,select4,args_byPar{1,i},3);
                mrs{run_sel,i}.data = data7;
                mrs{run_sel,i}.fit = mg_fit7;
                axis([1 5e7 0 max(data7.cdf(end,:))]);
                objs_v1_10(7,i) = timeWeightedObjective_v1_0(mg_fit7,data7.weights);
            case 8
                pmal = pmals(i,:);
                params8 = pmal;
                [mrs{run_sel,i},data8,mg_fit8] = runModelFunction_v5_0(datafile8,params8,conv_vol,Nx,dt,select4,args_byPar{2,i},3);
                mrs{run_sel,i}.data = data8;
                mrs{run_sel,i}.fit = mg_fit8;
                axis([1 5e7 0 max(data8.cdf(end,:))]);
                objs_v1_10(8,i) = timeWeightedObjective_v1_0(mg_fit8,data8.weights);
        end
    end
end
save([save_name '_' mfilename],'mrs');

endt = clock();
duramins = etime(endt,startt)/60;

fprintf('%s took %3.2f minutes\n',mfilename,duramins);